#include "StdAfx.h"
#include "HistoryStringCal.h"

HistoryStringCal::HistoryStringCal(void)
{
}

HistoryStringCal::~HistoryStringCal(void)
{
}

void HistoryStringCal::SortStringsByUseTime()
{
	int n = m_HistoryStringList.GetSize();
	CArray<HistoryString,HistoryString&> HistoryStringListNew;

	HistoryString kmin;
	int jmin;

	HistoryStringListNew.Append(m_HistoryStringList);

	for(int i=0;i<n-1;i++)
	{
		kmin = HistoryStringListNew[i];
		jmin = i;

		for(int j=i;j<n;j++)
		{
            if (kmin.useTime < HistoryStringListNew[j].useTime)
			//if(kmin.Times < HistoryStringListNew[j].Times)
			{
				kmin=HistoryStringListNew[j];
				jmin=j;
			}
		}
		if(jmin!=i) 
		{
			HistoryStringListNew[jmin]=HistoryStringListNew[i];
			HistoryStringListNew[i]=kmin;
		}
	}

	m_HistoryStringList.RemoveAll();
	m_HistoryStringList.Append(HistoryStringListNew);
}

void HistoryStringCal::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{	
		// storing code
		int n = m_HistoryStringList.GetSize();
        ar << n;
		for (int i = 0; i < n ; i++)
		{
            ar << m_HistoryStringList[i].cs;
            ar << m_HistoryStringList[i].Times;
            ar << m_HistoryStringList[i].useTime;
		}
	}
	else
	{	// loading code
		int len=0;
		ar >> len;

		m_HistoryStringList.RemoveAll();
        for (int i = 0; i < len ; i++)
        {
            HistoryString stt;
            ar >> stt.cs;
            ar >> stt.Times;
            ar >> stt.useTime;
            m_HistoryStringList.Add(stt);
        }
	}
}

bool HistoryStringCal::IsHistoryStringNeedSave(CString cs)
{
    int id1 = 0, id2 = 0;

    if (PublicFunc::IsStrHasStr(cs, _T("RoomStair"), id1, id2))
    {
        return false;
    }

    if (PublicFunc::IsStrHasStr(cs, _T("C:\\_Apps_\\MobileDgnSdk12\\"), id1, id2))
    {
        return true;
    }
    else if (PublicFunc::IsStrHasStr(cs, _T("C:\\tfs\\JV-PB\\MobileDgnSdk12\\"), id1, id2))
    {
        return true;
    }
    else if (PublicFunc::IsStrHasStr(cs, _T("D:\\WORK_SOURCE\\MakeDbxFiles\\"), id1, id2))
    {
        return true;
    }

    return false;
}

bool HistoryStringCal::GetValidStrings()
{
    CArray<HistoryString, HistoryString&> HistoryStringList;

    for (int i = 0; i < m_HistoryStringList.GetSize(); i++)
    {
        HistoryString stt = m_HistoryStringList[i];

        if (IsHistoryStringNeedSave(stt.cs))
        {
            HistoryStringList.Add(stt);
        }
    }

    m_HistoryStringList.RemoveAll();
    m_HistoryStringList.Append(HistoryStringList);

    return true;
}

void HistoryStringCal::AppendString(CString cs)
{
    if (cs != _T(""))
    {
        BOOL bHas = FALSE;
        for (int i = 0; i < m_HistoryStringList.GetSize(); i++)
        {
            HistoryString stt = m_HistoryStringList[i];
            if (stt.cs == cs)
            {
                m_HistoryStringList[i].Times++;
                m_HistoryStringList[i].useTime = CTime::GetCurrentTime();
                bHas = TRUE;
                break;
            }
        }

        if (!bHas)
        {
            HistoryString da;
            da.cs = cs;
            da.Times = 1;
            da.useTime = CTime::GetCurrentTime();

            m_HistoryStringList.Add(da);
        }
    }
}

void HistoryStringCal::GetSortedStringsByTimes(CArray<CString, CString&> &CStringList)
{
    SortStringsByUseTime();

    CStringList.RemoveAll();
    for (int i = 0; i < m_HistoryStringList.GetSize(); i++)
    {
        HistoryString stt = m_HistoryStringList[i];
        CStringList.Add(stt.cs);
    }
}
